package com.scoy.honeymei.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.mall.OrderDetailActivity;
import com.scoy.honeymei.bean.MallOrderBean;
import com.scoy.honeymei.utils.GlideImgUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ClassName: NormalAdapter$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateRemark: 更新内容
 */
public class MallMeAdapter extends RecyclerView.Adapter {
    private Context context;
    private ArrayList<MallOrderBean> datalist;
    private TextView notv;
    private int tPos;

    public MallMeAdapter(Context context, ArrayList<MallOrderBean> datalist, TextView notv, int tPos) {
        this.context = context;
        this.datalist = datalist;
        this.notv = notv;
        this.tPos = tPos;
    }

    public interface OnOneClick {
        void oneClick(int pos);
    }

    OnOneClick onOneClick;

    public void setOnOneClick(OnOneClick onOneClick) {
        this.onOneClick = onOneClick;
    }

    public interface OnTwoClick {
        void twoClick(int pos);
    }

    OnTwoClick onTwoClick;

    public void setOnTwoClick(OnTwoClick onTwoClick) {
        this.onTwoClick = onTwoClick;
    }

    public interface OnThreeClick {
        void threeClick(int pos);
    }

    OnThreeClick onThreeClick;

    public void setOnThreeClick(OnThreeClick onThreeClick) {
        this.onThreeClick = onThreeClick;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_mall_order, parent, false);
        return new OrderMeHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        OrderMeHolder meHolder = (OrderMeHolder) holder;
        MallOrderBean mallOrderBean = datalist.get(position);

        //订单状态:1=待付款,2=待发货,3=已发货,4=已完成,5=已评价
        String status = mallOrderBean.getStatus();

        if (tPos == 0) {
            meHolder.icaShopnameTv.setText(R.string.prepay);
        } else if (tPos == 1) {
            meHolder.icaShopnameTv.setText(R.string.prerecive);
            meHolder.mloBtn1Tv.setVisibility(View.GONE);
            meHolder.mloBtn2Tv.setText("确认收货");
        } else if (tPos == 2) {
            meHolder.icaShopnameTv.setText(R.string.finish);

            meHolder.molBtnCl.setVisibility(View.VISIBLE);
            meHolder.mloBtn1Tv.setVisibility(View.VISIBLE);
            if ("4".equals(status)) {
                meHolder.mloBtn1Tv.setVisibility(View.GONE);
                meHolder.mloBtn2Tv.setText("立即评价");
            } else if ("5".equals(status)) {
                meHolder.molBtnCl.setVisibility(View.GONE);
            }
        }


        List<MallOrderBean.GoodsListBean> goodsList = mallOrderBean.getGoodsList();
        int size = goodsList.size();

        meHolder.molOneCl.setVisibility(View.VISIBLE);
        meHolder.mloNum1Tv.setVisibility(View.VISIBLE);
        meHolder.mloNum0Tv.setVisibility(View.VISIBLE);
        if (size == 1) {
            meHolder.mloNum1Tv.setVisibility(View.GONE);
            meHolder.mloRv.setVisibility(View.GONE);
            MallOrderBean.GoodsListBean goodsListBean = goodsList.get(0);
            GlideImgUtil.glidePic(context, goodsListBean.getImage(), meHolder.cartImgIv);
            meHolder.cartNameTv.setText(goodsListBean.getName());
            meHolder.cartPriceTv.setText(goodsListBean.getMoney());
            meHolder.cartBuynumTv.setText("x" + mallOrderBean.getNumber());
            meHolder.mloNum0Tv.setText("共" + mallOrderBean.getNumber() + "件");
            meHolder.mloAllmoneyTv.setText("¥" + mallOrderBean.getPay_price());
        } else {
            meHolder.molOneCl.setVisibility(View.GONE);
            meHolder.mloNum0Tv.setVisibility(View.GONE);
            meHolder.mloRv.setVisibility(View.VISIBLE);

            ArrayList<MallOrderBean.GoodsListBean> glist = new ArrayList<>();
            glist.addAll(goodsList);
            OrderImgAdapter orderImgAdapter = new OrderImgAdapter(context, glist);
            meHolder.mloRv.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false));
            meHolder.mloRv.setAdapter(orderImgAdapter);

            meHolder.mloNum1Tv.setText("共" + mallOrderBean.getNumber() + "件");
            meHolder.mloAllmoneyTv.setText("¥" + mallOrderBean.getPay_price());
        }
        meHolder.itemOrderLlt.setOnClickListener(view -> {
            if (onOneClick != null) {
                onOneClick.oneClick(position);
            }
        });
        //cancle
        meHolder.mloBtn1Tv.setOnClickListener(v -> {
            if (onTwoClick != null) {
                onTwoClick.twoClick(position);
            }
        });
        //sure
        meHolder.mloBtn2Tv.setOnClickListener(v -> {
            if (onThreeClick != null) {
                onThreeClick.threeClick(position);
            }
        });
    }

    public ArrayList<MallOrderBean> getDatalist() {
        return datalist;
    }

    @Override
    public int getItemCount() {
        int iCount = datalist == null ? 0 : datalist.size();
        if (iCount == 0) {
            notv.setVisibility(View.VISIBLE);
        } else {
            notv.setVisibility(View.GONE);
        }
        return iCount;
    }

    static
    class OrderMeHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ica_shopname_tv)
        TextView icaShopnameTv;
        @BindView(R.id.cart_img_iv)
        ImageView cartImgIv;
        @BindView(R.id.cart_name_tv)
        TextView cartNameTv;
        @BindView(R.id.cart_pricesign_tv)
        TextView cartPricesignTv;
        @BindView(R.id.cart_price_tv)
        TextView cartPriceTv;
        @BindView(R.id.cart_buynum_tv)
        TextView cartBuynumTv;
        @BindView(R.id.mlo_rv)
        RecyclerView mloRv;
        @BindView(R.id.mlo_tv0)
        TextView mloTv0;
        @BindView(R.id.mlo_num0_tv)
        TextView mloNum0Tv;
        @BindView(R.id.mlo_num1_tv)
        TextView mloNum1Tv;
        @BindView(R.id.mlo_allmoney_tv)
        TextView mloAllmoneyTv;
        @BindView(R.id.mlo_btn1_tv)
        TextView mloBtn1Tv;
        @BindView(R.id.mlo_btn2_tv)
        TextView mloBtn2Tv;
        @BindView(R.id.mol_btn_cl)
        ConstraintLayout molBtnCl;
        @BindView(R.id.item_order_llt)
        LinearLayout itemOrderLlt;
        @BindView(R.id.mol_one_cl)
        ConstraintLayout molOneCl;

        OrderMeHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
